/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * NewMoviePage.java
 *
 * Created on Jun 29, 2010, 11:13:11 PM
 */

package movietracker;

import java.sql.SQLException;
import javax.swing.JOptionPane;

/**
 *
 * @author CaronJB
 */
public class NewMoviePage extends javax.swing.JDialog {

    /** Creates new form NewMoviePage */
    public NewMoviePage(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }
    public NewMoviePage()
    {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainLabel = new javax.swing.JLabel();
        descrLabel = new javax.swing.JLabel();
        titleLabel = new javax.swing.JLabel();
        titleTextField = new javax.swing.JTextField();
        genreLabel = new javax.swing.JLabel();
        genreTextField = new javax.swing.JTextField();
        releaseLabel = new javax.swing.JLabel();
        releaseTextField = new javax.swing.JTextField();
        enterButton = new javax.swing.JButton();
        closeButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(movietracker.MovieTrackerApp.class).getContext().getResourceMap(NewMoviePage.class);
        mainLabel.setFont(resourceMap.getFont("mainLabel.font")); // NOI18N
        mainLabel.setText(resourceMap.getString("mainLabel.text")); // NOI18N
        mainLabel.setName("mainLabel"); // NOI18N

        descrLabel.setText(resourceMap.getString("descrLabel.text")); // NOI18N
        descrLabel.setName("descrLabel"); // NOI18N

        titleLabel.setFont(resourceMap.getFont("titleLabel.font")); // NOI18N
        titleLabel.setText(resourceMap.getString("titleLabel.text")); // NOI18N
        titleLabel.setName("titleLabel"); // NOI18N

        titleTextField.setText(resourceMap.getString("titleTextField.text")); // NOI18N
        titleTextField.setName("titleTextField"); // NOI18N

        genreLabel.setFont(resourceMap.getFont("genreLabel.font")); // NOI18N
        genreLabel.setText(resourceMap.getString("genreLabel.text")); // NOI18N
        genreLabel.setName("genreLabel"); // NOI18N

        genreTextField.setText(resourceMap.getString("genreTextField.text")); // NOI18N
        genreTextField.setName("genreTextField"); // NOI18N

        releaseLabel.setFont(resourceMap.getFont("releaseLabel.font")); // NOI18N
        releaseLabel.setText(resourceMap.getString("releaseLabel.text")); // NOI18N
        releaseLabel.setName("releaseLabel"); // NOI18N

        releaseTextField.setText(resourceMap.getString("releaseTextField.text")); // NOI18N
        releaseTextField.setName("releaseTextField"); // NOI18N

        enterButton.setText(resourceMap.getString("enterButton.text")); // NOI18N
        enterButton.setName("enterButton"); // NOI18N
        enterButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                enterButtonActionPerformed(evt);
            }
        });

        closeButton.setText(resourceMap.getString("closeButton.text")); // NOI18N
        closeButton.setName("closeButton"); // NOI18N
        closeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                closeButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(99, 99, 99)
                        .addComponent(mainLabel))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(titleLabel)
                        .addGap(27, 27, 27)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(titleTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 265, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(descrLabel)))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(genreLabel)
                                .addGap(18, 18, 18)
                                .addComponent(genreTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(releaseLabel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(enterButton, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(18, 18, 18)
                                        .addComponent(closeButton, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addComponent(releaseTextField))))))
                .addContainerGap(43, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(mainLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(descrLabel)
                .addGap(46, 46, 46)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(titleLabel)
                    .addComponent(titleTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(51, 51, 51)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(genreLabel)
                    .addComponent(genreTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(56, 56, 56)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(releaseLabel)
                    .addComponent(releaseTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(47, 47, 47)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(enterButton)
                    .addComponent(closeButton))
                .addContainerGap(35, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void closeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeButtonActionPerformed
        // TODO add your handling code here:
        NewMoviePage.this.setVisible(false);
    }//GEN-LAST:event_closeButtonActionPerformed

    private void enterButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_enterButtonActionPerformed
        // TODO add your handling code here:
        Database D = new Database();
        D.dataBaseConnect();

        String title = titleTextField.getText();
        String genre = genreTextField.getText();
        String Stryear = releaseTextField.getText();
        int available = 0;
        String borrower = "NULL";

        if(title.isEmpty() == true || genre.isEmpty() == true || Stryear.isEmpty() == true)
        {
            JOptionPane.showMessageDialog(null, "Please fill in ALL fields");
        }//end of if
        else
        {
            int year = Integer.parseInt(Stryear);
            String myState = "insert into movies values('" + title + "', '" + genre + "', " + year + ", " + available + ", '" + borrower + "')";

            //if to check if its a valid insert
            if(!D.executeStatement(myState))
            {
               //actual SQL insert did not work, error
               System.out.println("movie insert failed");
               JOptionPane.showMessageDialog(null, "SQL Insert Failed\n Check for duplicates or wrong info");
            }//end if

            titleTextField.setText("");
            genreTextField.setText("");
            releaseTextField.setText("");
        }//end of else
    }//GEN-LAST:event_enterButtonActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                NewMoviePage dialog = new NewMoviePage(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton closeButton;
    private javax.swing.JLabel descrLabel;
    private javax.swing.JButton enterButton;
    private javax.swing.JLabel genreLabel;
    private javax.swing.JTextField genreTextField;
    private javax.swing.JLabel mainLabel;
    private javax.swing.JLabel releaseLabel;
    private javax.swing.JTextField releaseTextField;
    private javax.swing.JLabel titleLabel;
    private javax.swing.JTextField titleTextField;
    // End of variables declaration//GEN-END:variables

}
